/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LabelFilterPanel.java
 *
 * Created on 22.Şub.2010, 14:40:53
 */
package org.testis.gui.mvc.view.labels;

import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.controller.testdesign.LabelPairSettingsController;
import org.testis.gui.mvc.view.AbstractViewPanel;
import org.testis.orm.labelhandler.LabelRefined;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author selcuk
 */
public class LabelPairSettingsView extends javax.swing.JPanel implements AbstractViewPanel {

    private LabelJTreeWithCheckbox m_labelTree = null;
    private LabelPairSettingsController m_controller;

    /** Creates new form TestSuiteItemLabelSettingsPanel */
    public LabelPairSettingsView() {
        m_controller = TestDesignControllerList.newInstance().getLabelPairSettingsController();
        m_controller.addView(this);
        initComponents();
        m_labelTree = (LabelJTreeWithCheckbox) jTree1;
    }

    public LabelPairSettingsView(List<Integer> selectedLabels) {
        initComponents();
        m_labelTree = (LabelJTreeWithCheckbox) jTree1;
        loadLabels(selectedLabels);
    }

    public void loadLabels(List<Integer> selectedLabels) {
        m_labelTree.loadLabels(selectedLabels);
    }

    public List<LabelRefined> getSelectedLabels() {
        return m_labelTree.getSelectedLabels();
    }

    public LinkedList<String> getSelectedLabelNames() {
        List<String> labelNameList = m_labelTree.getSelectedLabelNames();
        return new LinkedList<String>(labelNameList);
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                LabelPairSettingsController.LABELS_PROPERTY)) {
            HashMap labels = (HashMap) evt.getNewValue();
            loadLabels(new ArrayList<Integer>(labels.keySet()));
        } else if (evt.getPropertyName().equals(
                LabelPairSettingsController.ERROR_PROPERTY)) {
            String val = (String) evt.getNewValue();
            JOptionPane.showMessageDialog(
                    this,
                    val,
                    "Error Dialog",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new org.testis.gui.mvc.view.labels.LabelJTreeWithCheckbox();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        jScrollPane1.setViewportView(jTree1);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Demo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                LabelPairSettingsView view = new LabelPairSettingsView();
                frame.getContentPane().add(view);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
